//
//  WHNSuggestViewController.m
//  YiQiTingShu
//
//  Created by dllo on 15/11/11.
//  Copyright © 2015年 直到世界的尽头. All rights reserved.
//

#import "WHNSuggestViewController.h"
#import <SKPSMTPMessage.h>
#import "PublicHeader.h"
#import "PlayerButton.h"

@interface WHNSuggestViewController ()<UITextViewDelegate, UITextFieldDelegate, SKPSMTPMessageDelegate>
@property(nonatomic, strong)UITextView *textView;
@property(nonatomic, strong)UILabel *textLabel;
@property(nonatomic, strong)UITextField *QQTextField;
@property(nonatomic, strong)UILabel *label;


@end

@implementation WHNSuggestViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self.view NightWithType:UIViewColorTypeNormal];
    self.title = @"意见反馈";
    UIView *view = [[UIView alloc]initWithFrame:FitRectMake(0, 0, 375, 667)];
    [view NightWithType:UIViewColorTypeNormal];
    [self.view addSubview:view];
    [self.view addSubview:self.textView];
    [self.textView addSubview:self.textLabel];
    [self.view addSubview:self.QQTextField];
    [self.view addSubview:self.label];
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc]initWithTitle:@"发送" style:UIBarButtonItemStylePlain target:self action:@selector(sendMessage)];
    
}
-(UITextView *)textView
{
    if (!_textView) {
        _textView = [[UITextView alloc]initWithFrame:CGRectMake(20 * FitWidth, 20 * FitHeight, 335 * FitWidth, 150 * FitHeight)];
        _textView.layer.borderWidth = 1.0;
        _textView.layer.cornerRadius = 5;
        _textView.scrollEnabled = YES;
        _textView.delegate = self;
        _textView.font = [UIFont systemFontOfSize:15];
        _textView.returnKeyType = UIReturnKeySend;
        [_textView NightWithType:UIViewColorType3];
    }
    return _textView;
}
-(UILabel *)textLabel
{
    if (!_textLabel) {
        _textLabel = [[UILabel alloc]initWithFrame:CGRectMake(0, 10 * FitHeight, _textView.frame.size.width, 20 * FitHeight)];
        _textLabel.text = @"  写下您的反馈意见,我们会尽快处理";
        _textLabel.textColor = [UIColor lightGrayColor];
        _textLabel.font = [UIFont systemFontOfSize:17];
        _textLabel.backgroundColor = [UIColor clearColor];
    }
    return _textLabel;
}
-(UITextField *)QQTextField
{
    if (!_QQTextField) {
        _QQTextField = [[UITextField alloc]initWithFrame:CGRectMake(_textView.frame.origin.x, _textView.frame.origin.y + _textView.frame.size.height + 20 * FitHeight, _textView.frame.size.width, 40 * FitHeight)];
        _QQTextField.placeholder = @"  QQ或邮箱等联系方式";
        _QQTextField.layer.borderWidth = 1.0;
        _QQTextField.layer.cornerRadius = 5;
        _QQTextField.delegate = self;
        _QQTextField.clearButtonMode = UITextFieldViewModeWhileEditing;
        [_QQTextField NightWithType:UIViewColorType3];
        //        _QQTextField.returnKeyType = UIReturnKeySend;
    }
    return _QQTextField;
}
-(UILabel *)label
{
    if (!_label) {
        _label = [[UILabel alloc]initWithFrame:CGRectMake(_QQTextField.frame.origin.x, _QQTextField.frame.origin.y + _QQTextField.frame.size.height + 10 * FitHeight, _QQTextField.frame.size.width, 20 * FitHeight)];
        _label.text = @"您的联系方式有助于我们沟通和解决问题,仅工作人员可见";
        _label.textColor = [UIColor lightGrayColor];
        _label.font = [UIFont systemFontOfSize:12];
    }
    return _label;
}
#pragma mark 视图将出现时,上下隐藏
-(void)viewWillAppear:(BOOL)animated
{
    [PlayerButton shareButton].hidden = YES;
}
-(void)viewWillDisappear:(BOOL)animated
{
    [PlayerButton shareButton].hidden = NO;
    [_QQTextField resignFirstResponder];
}
-(void)sendMessage
{
    SKPSMTPMessage *myMessage = [[SKPSMTPMessage alloc]init];
    myMessage.fromEmail = @"rwzrwz449@163.com";
    myMessage.toEmail = @"2606568887@qq.com";
    myMessage.relayHost = @"smtp.163.com";
    myMessage.requiresAuth = YES;
    if (myMessage.requiresAuth) {
        myMessage.login =  @"rwzrwz449@163.com";
        myMessage.pass = @"rwzrwz449@";
    }
    myMessage.wantsSecure = YES;
    myMessage.subject = @"123";
    myMessage.delegate = self;
    NSDictionary *dic = [NSDictionary dictionaryWithObjectsAndKeys:@"text/plain;charset = UTF-8", kSKPSMTPPartContentTypeKey, [NSString stringWithFormat:@"%@_%@", _textView.text, _QQTextField.text], kSKPSMTPPartMessageKey, @"8bit", kSKPSMTPPartContentTransferEncodingKey, nil];
    myMessage.parts = [NSArray arrayWithObjects:dic, nil];
    [myMessage send];
    
    UIAlertController* alert = [UIAlertController alertControllerWithTitle:@"温心提示"message:@"发送成功.感谢您提出的宝贵意见,我们会努力做得更好!"preferredStyle:UIAlertControllerStyleAlert];
    UIAlertAction* defaultAction = [UIAlertAction actionWithTitle:@"OK" style:UIAlertActionStyleDefault handler:^(UIAlertAction * action) {
        [_textView resignFirstResponder];
        [_QQTextField resignFirstResponder];
        [self.view endEditing:YES];
        [self.navigationController popViewControllerAnimated:YES];
    }];
    
    [alert addAction:defaultAction];
    [self presentViewController:alert animated:YES completion:nil];
    
}

-(BOOL)textFieldShouldReturn:(UITextField *)textField
{
    [_textView resignFirstResponder];
    [_QQTextField resignFirstResponder];
    return YES;
}
-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
    [self.view endEditing:YES];
}

-(void)messageSent:(SKPSMTPMessage *)message
{
    
}
-(void)messageFailed:(SKPSMTPMessage *)message error:(NSError *)error
{
    
}

-(void)textViewDidBeginEditing:(UITextView *)textView
{
    [_textLabel removeFromSuperview];
}

@end
